To navigate files and directories in operating systems you can use GUI or command-line interface (through shell). These files and directories are organized in a hierarchical directory tree (main directory branches off and holds other directories and files).
the path to the root directory (the parent for all other directories) is denoted by /
root directory contains:
xxxxxxxxxx/bin : essential binaries and programs/etc : system configuration files/home : users files and directories/opt : package-manager installed files/proc : information about currently running processes/root : administrator account files and directories/sbin : important system binaries and programs/tmp : temporary files/usr : user installed software/var : system logs
subdirectories are separated by / (forward slash)
hidden files and directories start with a . (e.g., .hidden_file)
most common command-line interface is bash (reference manual)
strings in bash can be denoted by 'my file' or by my\ file (\ is the escape character)
tab completion and wildcards are available in bash
xxxxxxxxxx<user>@<computer>:<path>$ <command> <options> <arguments>xxxxxxxxxxman <command> : show manual for command (or info <command>)<command> --help : show available flags for commandecho <str> : print inputclear : clear screen (or ctrl + L)history : show command history (or ctrl + R for search)reset : reinitialize terminalexit : close terminalreboot : reboot computershutdown : shutdown computerpwd : print working directoryls <path> : list files and directories (-l list, -a all, -h human readable, -d directories)tree <path> : list files and directories in a tree-like formatcd <path> : change directorytouch <path1> ... : create files (file{1..5}.txt for similar names)mkdir <path1> ... : create directories (dir{1..5} for similar names)rm <path1> ... : remove files/directories (-r for directories, -f force)cp <path1> <path2> : copy file/directory (-r for directory)mv <path1> <path2> : move (or rename) file/directorymc : Midnight Commander file manager
System info:
xxxxxxxxxxuname -a : OS info (or lsb_release -a)lscpu : CPU infofree -h : RAM infoip addr : network infodf -h : storage info (du -cksh for folders, --max-depth 1)
Wildcards (symbols used to represent one or more characters):
xxxxxxxxxx~ : /home/user/ : / (i.e., root directory).. : one level up. : current directory- : back? : any symbol* : any number of any symbols
Terminal shortcuts:
| shortcut | description |
|---|---|
| Ctrl + Shift + C / V | copy/paste |
| Ctrl + A / E | move cursor to the begining/end of line |
| Ctrl + W | delete a word to the left |
| Ctrl + U / K | delete everything to the left/right of a cursor |
| Alt + B / F | move one word back/forward |
| Tab | tab completion |
| Ctrl + Shift + T | open new terminal tab |
| Ctrl + Shift + W | close current terminal tab |
| Alt + <#> | swith to <#>th terminal tab |
| Ctrl + L | clear terminal screen |
| Ctrl + C | kill current process |
| Ctrl + Z | suspend current process |
To make command history be based on relevancy (instead of chronological order) add to ~/.inputrc the following:
xxxxxxxxxx"\e[A": history-search-backward"\e[B": history-search-forward"\e[C": forward-char"\e[D": backward-char
xxxxxxxxxx<program> <file> : execute file (or ./<file>)sh <file> : execute shell filecat <file> : view contents of a filehead/tail <file> : view first/last 10 lines of a fileless <file> : open file with less text editornano <file> : open file with nano text editorfind <path> -name <file> : search for a filegrep <str> <files> : search within files (can use regexes)wc <opt> <path> : count <opt> in a file (-l lines, -w words, -c characters)diff <path1> <path2> : compare files/directories
/dev/null)
<!-- graph LR id1(Standard Input) id2[Shell] id3(Standard Output) id4(Standard Error)
id1 -- #0 --> id2 id2 -- #1 --> id3 id2 -- #2 --> id4 -->
xxxxxxxxxx<com> < <file> : stdin from file<com> > <file> : stdout to file (rewrite)<com> >> <file> : stdout to file (append)<com> 2> <file> : stderr to file (rewrite)<com> 2>> <file> : stderr to file (append)<com1> | <com2> : pipeline (stdin[i+1] = stdout[i])
xxxxxxxxxxsudo <command> : run command as rootsudo su - : substitute user to root (exit: to log out)users : show currently logged in usersgroups <user> : show user groupsuseradd <user> : add new useruserdel <user> : delete user (-r with their home directory)passwd <user> : change user passwordpasswd -e <user> : expire user password (to force user change it on the next log in)
/etc/group. Each line represents a group and has four fields separated by colons: group name, group password (defaults to the root password, usually encrypted and stored in a separate file), group ID, and list of users in the group./etc/passwd (most users in Linux are just running processes associated with a user). Each line represents a user; first three fields: username, password (encrypted and stored in a separate file), and UID (user ID, e.g., for root UID=0). Users with UID < 1000 are system processes, and users with UID > 1000 are real.etc/shadow, which can only be read by root.There are three different types of permissions: read, write and execute. To view files/directories permissions use ls -l:
xxxxxxxxxxuser@linux:~$ ls -ltotal 10Kdrwxrwxr-x <N> <user> <group> <size> <date> <name>xxxxxxxxxxd/l/- : directory/link/filerwx/--- : read/write/execute for user/group/all other usersN : number of hardlinks or directories inside this directoryuser : owner of a file/directorygroup : owner groupsize : size of a file/directorydate : last modifiedname : name of a file/directory
xxxxxxxxxxchmod ### <path> : change file/directory permissions, where ### are1) 3 symbols: [ugoa][+-=][rwx]ugoa (user, group, other, all)+-= (add, remove, assign)rwx (read, write, execute)2) 3 numbers: 4 - read, 2 - write, 1 - execute(add these numbers to set permissions)chown <user> <path> : change the owner of a file/directorychgrp <group> <path> : change the group file/directory belongs toExamples:u+x (execute for the owner)ug=rw,o=r (read and write for the owner and their group, read for other users)a-x (remove execute for all users)g=u (set group permissions as owner permissions)754 (rwxr-xr--)
| Base-8 | Base-2 | Permissions |
|---|---|---|
| 0 | 000 | --- |
| 1 | 001 | --x |
| 2 | 010 | -w- |
| 3 | 011 | -wx |
| 4 | 100 | r-- |
| 5 | 101 | r-x |
| 6 | 110 | rw- |
| 7 | 111 | rwx |
Sometimes you want to allow users do actions that require root privileges without giving them root access (e.g., to change user password you need to write into etc/shadow which is owned by root). For this reason special permissions exist:
setuid
s instead of x in its list of owner permissionssetgid
s instead of x in its list of group permissionssticky bit
t instead of x in its list of permissions for other usersxxxxxxxxxxsudo chmod u+s <path> : set setuid flag for a file (or 4###)sudo chmod g+s <path> : set setgid flag for a file (or 2###)sudo chmod +t <path> : set sticky bit flag for a file/directory (or 1###)
Developers package software using software compiling tools. Different Linux distributions can use different methods for software packaging, e.g., Red Hat uses .rpm (Red Hat package manager) packages and Ubuntu uses .deb (Debian) packages. They contain instructions for a computer to perform, computer code and other files that program might use.
xxxxxxxxxxsudo dpkg -i <name.deb> : install debian packagesudo dpkg -r <name> : remove debian packagedpkg -l : list installed debian packages
Packages usually rely on other pieces of code in order to work. In Linux these dependencies can be other packages or shared libraries. Standalone packages (e.g., .deb packages) don't install neccesary dependencies automatically, that's why package managers exist.
A package manager makes sure that the process of software installation, removal, update, and dependency management is as easy and automatic as possible. APT (advanced package tool) is a default package manager for Ubuntu.
/etc/apt/sources.listxxxxxxxxxxsudo apt install <name> : install package (with dependencies)sudo apt remove <name> : remove package (with dependencies)sudo apt purge <name> : remove package and its config files (with dependencies)sudo apt autoremove : remove unnecessary packagessudo apt update : update the list of packages for upgradesudo apt upgrade : upgrade installed packages
Archive is one or more files compressed into a single file.
.tar, .zip, and .rar.xxxxxxxxxxzip <path> ... : create .zip archive (-r directory, -e encrypt)unzip <path> : extract .zip archivegzip ... : create .gz archives and delete originals (-c don't delete)gunzip ... : extract .gz archives and delete originals (-c don't delete)tar -cvf <path> ... : create .tar archive (no compression)tar -xvf <path> : extract .tar archivetar -czvf <path> ... : create .tar.gz archivetar -xzvf <path> : extract .tar.gz archivetar -cjvf <path> ... : create .bz2 archivetar -xjvf <path> : extract .bz2 archive
Mobile applications usually can be downloaded only from a trusted source (like an app store). App store is a central managed marketplace for app developers to publish and sell mobile apps, i.e., the app store acts as package manager, and the app store service acts as a package repository.
Driver is a software that helps a hardware device interact with an OS.
In Linux, everything is a file, even hardware devices. So when a device is connected to a computer, a device file is created in the /dev directory.
most of the device files don't correspond to physical devices (e.g., /dev/null)
common device types in Linux (see with ls -l):
mass storage devices (e.g., hard and flash drives) are denoted as /dev/sda, /dev/sdb/, etc.
most of the device drivers are a part of the kernel, and if not, there are kernel modules (installed like all the other software in Linux)
Installing latest system updates is a good practice to keep OS secure and get the newest features. In Ubuntu sudo apt upgrade will install the latest security updates, but won't upgrade the kernel and other core packages.
xxxxxxxxxxuname -r : show kernel releasesudo apt update : update the list of packages for upgradesudo apt full-upgrade : upgrade all packages including the kernel (or dist-upgrade)
A file system is used to keep track of files and file storage on a disk. The major operating systems have their own unique file systems:
Windows uses NTFS by default, and Linux uses ext4 (most common)
for most file systems, cross OS support is minimal (e.g., Windows doesn't read ext4).
there is also FAT32 file system (used for flash drives):
A storage device can be divided into partitions (pieces of the device that can be managed independently). Partitions essentially act as separate sub-devices, but they all use the same physical device.
Partition table is a component of a device that tells the OS how the device is partitioned (which are the boot partitions, space allocated for partitions, etc.)
There are two main partition table schemes which decide how to structure the information on partitions:
MBR (master boot record)
GPT (GUID partition table)
In Linux, disk partitioning and file system formatting can be done via GUI or with a few different partitioning terminal tools, e.g., with parted:
xxxxxxxxxxsudo parted -l : show info about connected storage devices and their partitionssudo parted /dev/sdx : run parted in an interactive mode for a selected device(parted) print : show info about storage device and its partitions(parted) mklabel ... : create device label (e.g., mklabel msdos/gpt)(parted) mkpart ... : create partition (e.g., mkpart primary ext4 1MiB 5GiB)(parted) quit : quit from interactive modesudo mkfs -t <FS> <sdx#> : format selected partition with a file system
After a file system has been formatted it needs to be mounted to a directory (to make it accessible). Linux does this automatically, but it can also be done manually.
/etc/fstab (contains device UUIDs, or universally unique IDs; mount points, type of used file system, etc.)xxxxxxxxxxsudo mount <sdx#> <path> : mount storage device to a directorysudo umount <sdx#> : unmount storage device (or <path> instead of </dev/sdx#>)sudo blkid : show UUIDs for storage devices (a.k.a block devices)
Virtual memory allows OS provide the available physical memory (RAM) to the running applications. It creates a mapping between virtual and physical addresses. Virtual memory allows programs:

When a particular page of data (data block) isn't being used by an application, it gets evicted (copied out of memory onto the hard drive). This way memory resources are used most efficiently, and if a program needs a page that's not accessed a lot, the OS can still get to it in swap.
Manually create swap partition on a storage device:
xxxxxxxxxxsudo parted /dev/sdx(parted) mkpart primary linux-swap <start> <end>(parted) quitsudo mkswap /dev/sdx#sudo swapon /dev/sdx#
Linux uses a structure called inode to store and represent files and their metadata on a volume.
inodes are stored in an inode table
the inode stores everything about files except file names and the file data itself
there are special types of files that provide access to other files:
Symbolic links
ln -s <path> <softlink>Hard links
ln <path> <hardlink>ls -l will show the amount of hardlinks a file has (third field)In Linux, disk usage and disk free utilities can be used to monitor disk usage. Linux does a better job than Windows in avoiding fragmentation of data on hard disk drives, so defragmentation is not needed.
xxxxxxxxxxdu <path> : show disk usage for a directory (-h human readable)df : show free disk space (-h human readable)
Data corruption could happen for lots of reasons:
Linux file system has features that minimize the danger of data corruption, as well as, features that recover data when it gets damaged:
sudo fsck /dev/sdx only on unmounted devices!)fsck runs on a boot and tries to auto-repair any issues with the file systemProgram is an application that a user can run.
Process is a program that's executing (i.e., user can have many processes of the same program running at the same time, e.g., browser tabs of a web browser).
init process (PID = 1), which starts up all other processesIn Linux processes can be monitored:
/proc directory (since everything in Linux has a file, including processes)ps utilityxxxxxxxxxxps : show short list of processesps -x : show list of processesps -ef : show extended list of processes
Info for ps -x:
Info for ps -ef:
Sometimes user might want to interrupt a process before it fully completes. Signals are used for that purpose, they can be generated through other processes and software, or with keyboard shortcuts. Most common signals in Linux:
SIGINT
SIGTERM
kill <PID>SIGKILL
kill -KILL <PID>SIGTSTP
kill -TSTP <PID>SIGCONT
kill -CONT <PID>In Linux, job is a task that has started running and not yet completed.
each job is assigned with a sequential job ID
types of job statuses:
user can turn foreground jobs into background jobs by suspending them, i.e., with a Ctrl + Z shortcut, or with a bg command
user can run a program in a background mode with a <program> & command
xxxxxxxxxxjobs : list all jobsfg %<JID>/%<str> : place job into the foregroundbg %<JID>/%<str> : place job in the background
In Linux, user can monitor system resources use with several tools:
uptime (brief info)
top (full info)
htop (similar to top)
lsof
SSH (secure shell) is a protocol used to securely connect to computers remotely.
SSH client needs to be installed on a user computer, and SSH server on a host machine (popular programs: OpenSSH and PuTTy)
SSH server doesn't need to be a physical machine, it can be just a software that's running as a background process
connect with ssh <user>@<host> (OpenSSH)
-p <port> (22 by default for SSH)when user connects to a remote machine for the first time, they will be asked to verify the authenticity of a host, and after confirmation host will be added to the list of known hosts
SSH authentication keys can be used instead of passwords (more secure)
users can transfer data between computers through SSH tunnel using scp (secure copy) utility or file managers (e.g., filezilla):
xxxxxxxxxxscp <user>@<host>:<path1> <path2> : copy server → clientscp <path1> <user>@<host>:<path2> : copy client → server
another option for secure remote connections: VPN
In most systems, there is a service that runs in the background and constantly writes events to logs. In Linux, logs are stored in the /var/log directory.
some of the log files:
xxxxxxxxxx/var/log/syslog : everything (except off events)/var/log/auth.log : authorization and security-related events/var/log/kern.log : kernel messages/var/log/dmesg : system startup messages
logs are deleted after some period of time to save storage space (lofrotate is a utility for log rotation)
in the case of a set of machines the centralized logging is used
/var/log/syslog includes:
1970-01-01 00:00)can search for specific events by filtering
watch logs in real time: tail -f /var/log/syslog
Installing an OS on a large number of machines using traditional methods (e.g., with a USB stick) can be very time consuming, so other methods are used instead:
Disk cloning
umount /dev/sdxsudo dd if=dev/sdx of=<path.img> bs=100MNetwork initiated deployment
xxxxxxxxxxnmcli networking on/off : networking on/offnmcli radio wifi on/off : wi-fi on/offip a : show local IP addresscurl ifconfig.me : show IP addressping <domain>/<IP> : send an echo requesttraceroute <domain>/<IP> : trace route to the destinationnslookup <domain>/<IP> : resolve a domain namewget <options> <link> : download a file--spider - check file availability-P <path> - specify download directory-O <path> - specify name of a file-i <file.txt> - download files using links in a text file-r -l <depth> - download recursively-A <ext1>,... - list of allowed file extensions-R <ext1>,... - list of rejected file extensions
Users can create aliases for their most used commands. Aliases can be temporary (for a current terminal window) or permanent (modify ~/.bashrc).
xxxxxxxxxxalias : list all aliasesalias <alias>="<com>" : create aliasunalias <alias> : remove alias (-a all)
In Linux, environment variables are a set of dynamic named values, that are used to store system settings (i.e., path to home directory, language settings, terminal settings, etc.). They are used by applications launched in shells or subshells.
environment variables have the following format:
xxxxxxxxxxKEY=valueKEY="value"KEY=value1:value2
environment variable types:
show and modify environment variables values:
xxxxxxxxxxenv : list all env variables (or printenv)set : list all env and shell variablesenv $<var> : print env variable value (or printenv <var>)echo $<var> : print env or shell variable value<var>="<val>" : assign value to a shell variableexport <var>="<val>" : assign value to an env variableunset <var> : unset env or shell variable
to make changes in environment variables persistent user needs to modify bash configuration files:
/etc/environment in the VAR="val" format~/.bashrc in the export VAR="val" format (user-specific)